package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.fragment.CommodityFragment;
import com.luck.easyfoodbatch.fragment.ShopFragment;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCollectionActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.left_back)
    ImageButton leftBack;
    @BindView(R.id.rab1)
    RadioButton rab1;
    @BindView(R.id.rab2)
    RadioButton rab2;
    @BindView(R.id.rabs)
    RadioGroup rabs;
    @BindView(R.id.vp)
    ViewPager vp;
    private int currIndex = 0;// 当前页卡编号
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private CommodityFragment commodityFragment;
    private ShopFragment shopFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection);
        App.getInstance().addActivity(MyCollectionActivity.this);
        ButterKnife.bind(this);

        commodityFragment = new CommodityFragment();
        shopFragment = new ShopFragment();
        fragments.add(commodityFragment);
        fragments.add(shopFragment);
        rabs.setOnCheckedChangeListener(this);
        vp.addOnPageChangeListener(new MyOnPageChangeListener());
        vp.setCurrentItem(currIndex);
        vp.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments));
    }

    @OnClick(R.id.left_back)
    public void onClick() {
        finish();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rab1:
                currIndex = 0;
                break;
            case R.id.rab2:
                currIndex = 1;
                break;
        }
        vp.setCurrentItem(currIndex);
    }



    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageSelected(int arg0) {
            currIndex = arg0;
            switch (arg0) {
                case 0:
                    rab1.setChecked(true);
                    break;
                case 1:
                    rab2.setChecked(true);
                    break;
            }
            vp.setCurrentItem(currIndex);
        }
    }

    public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }

    }
}
